package com.colin.app.activity.other;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.colin.app.R;
import com.colin.app.app.AppActivity;
import com.colin.app.app.BasePresenterImpl;
import com.colin.app.bean.DemoBean;
import com.colin.app.bean.ItemBean;
import com.colin.app.service.MyAccessibilityService;
import com.colin.library.help.TimeHelp;
import com.colin.library.view.help.MetricsHelp;
import com.google.common.base.StandardSystemProperty;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Colin on 2017/2/7.
 */

public class OtherPresenterImpl extends BasePresenterImpl implements OtherContract.OtherPresenter {

    private OtherContract.OtherView mOtherView;
    private OtherContract.OtherModel mOtherModel;

    public OtherPresenterImpl(AppActivity mAppActivity, OtherContract.OtherView mOtherView, DemoBean demoBean) {
        super(mAppActivity, mOtherView);
        this.mOtherView = mOtherView;
        this.mOtherModel = new OtherModelImpl(demoBean);
    }

    @Override
    public void setData() {
        mOtherView.setTitle(mOtherModel.getTitle());
    }


    @Override
    public void getBuildMessage(TextView text_build_message) {
        StringBuffer stringBuffer = new StringBuffer("Build:" + '\n');
        stringBuffer.append("主板-->>" + Build.BOARD + '\n');
        stringBuffer.append("系统定制商-->>" + Build.BRAND + '\n');
        if (Build.VERSION.SDK_INT > 21) {
            if (null != Build.SUPPORTED_ABIS && Build.SUPPORTED_ABIS.length > 0) {
                for (String supportedAbi : Build.SUPPORTED_ABIS) {
                    stringBuffer.append("CPU指令集-->>" + supportedAbi + '\n');
                }
            }
        }
        stringBuffer.append("设备参数-->>" + Build.DEVICE + '\n');
        stringBuffer.append("显示屏参数-->>" + Build.DISPLAY + '\n');
        stringBuffer.append("唯一编号-->>" + Build.FINGERPRINT + '\n');
        stringBuffer.append("硬件序列号-->>" + Build.SERIAL + '\n');
        stringBuffer.append("修订版本列表-->>" + Build.ID + '\n');
        stringBuffer.append("硬件制造商-->>" + Build.MANUFACTURER + '\n');
        stringBuffer.append("版本-->>" + Build.MODEL + '\n');
        stringBuffer.append("硬件名-->>" + Build.HARDWARE + '\n');
        stringBuffer.append("手机产品名-->>" + Build.PRODUCT + '\n');
        stringBuffer.append("描述Build的标签-->>" + Build.TAGS + '\n');
        stringBuffer.append("Builder类型-->>" + Build.TYPE + '\n');
        stringBuffer.append("当前开发代号-->>" + Build.VERSION.CODENAME + '\n');
        stringBuffer.append("源码控制版本号-->>" + Build.VERSION.INCREMENTAL + '\n');
        stringBuffer.append("版本字符串-->>" + Build.VERSION.RELEASE + '\n');
        stringBuffer.append("版本号-->>" + String.valueOf(Build.VERSION.SDK_INT) + '\n');
        stringBuffer.append("Host值-->>" + Build.HOST + '\n');
        stringBuffer.append("User名-->>" + Build.USER + '\n');
        stringBuffer.append("设备品牌-->>" + Build.BRAND + '\n');
        stringBuffer.append("编译时间-->>" + TimeHelp.getTimeString(Build.TIME) + '\n');

        stringBuffer.append("OS_NAME-->>" + StandardSystemProperty.OS_NAME + '\n');
        stringBuffer.append("OS_ARCH-->>" + StandardSystemProperty.OS_ARCH + '\n');
        stringBuffer.append("OS_VERSION-->>" + StandardSystemProperty.OS_VERSION + '\n');

        stringBuffer.append("os.version-->>" + System.getProperty("os.version") + '\n');
        stringBuffer.append("os.name-->>" + System.getProperty("os.name") + '\n');


        text_build_message.setText(stringBuffer.toString());
    }

    /**
     * View的坐标系统是相对于父控件而言的.
     * getTop();       //获取子View左上角距父View顶部的距离
     * getLeft();      //获取子View左上角距父View左侧的距离
     * getBottom();    //获取子View右下角距父View顶部的距离
     * getRight();     //获取子View右下角距父View左侧的距离
     * <p>
     * MotionEvent中 get 和 getRaw 的区别
     * event.getX();       //触摸点相对于其所在组件坐标系的坐标
     * event.getY();
     * <p>
     * event.getRawX();    //触摸点相对于屏幕默认坐标系的坐标
     * event.getRawY();
     *
     * @param view
     * @param event
     */
    @Override
    public void touchView(View view, MotionEvent event) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("View的高度:DP==" + String.valueOf(MetricsHelp.px2dp(mAppActivity, view.getHeight())) + '\n');
        stringBuffer.append("View的宽度:DP==" + String.valueOf(MetricsHelp.px2dp(mAppActivity, view.getWidth())) + '\n');
        stringBuffer.append("View的高度:PX==" + String.valueOf(view.getHeight()) + '\n');
        stringBuffer.append("View的宽度:PX==" + String.valueOf(view.getWidth()) + '\n');
        stringBuffer.append("View左上角距父View顶部的距离:" + String.valueOf(view.getTop()) + '\n');
        stringBuffer.append("View左上角距父View左侧的距离:" + String.valueOf(view.getLeft()) + '\n');
        stringBuffer.append("View右下角距父View顶部的距离:" + String.valueOf(view.getBottom()) + '\n');
        stringBuffer.append("View右下角距父View左侧的距离:" + String.valueOf(view.getRight()) + '\n');
        stringBuffer.append("触摸点相对于其所在组件坐标系的坐标X:" + String.valueOf(event.getX()) + '\n');
        stringBuffer.append("触摸点相对于其所在组件坐标系的坐标Y:" + String.valueOf(event.getY()) + '\n');
        stringBuffer.append("触摸点相对于屏幕默认坐标系的坐标X:" + String.valueOf(event.getRawX()) + '\n');
        stringBuffer.append("触摸点相对于屏幕默认坐标系的坐标Y:" + String.valueOf(event.getRawY()) + '\n');
        mOtherView.alertShow("控件坐标基本信息", stringBuffer.toString().trim());
    }

    @Override
    public void setViewData(View view, TextView textView) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("View的高度:DP==" + String.valueOf(MetricsHelp.px2dp(mAppActivity, view.getHeight())) + '\n');
        stringBuffer.append("View的宽度:DP==" + String.valueOf(MetricsHelp.px2dp(mAppActivity, view.getWidth())) + '\n');
        stringBuffer.append("View的高度:PX==" + String.valueOf(view.getHeight()) + '\n');
        stringBuffer.append("View的宽度:PX==" + String.valueOf(view.getWidth()) + '\n');
        stringBuffer.append("View左上角距父View顶部的距离:" + String.valueOf(view.getTop()) + '\n');
        stringBuffer.append("View左上角距父View左侧的距离:" + String.valueOf(view.getLeft()) + '\n');
        stringBuffer.append("View右下角距父View顶部的距离:" + String.valueOf(view.getBottom()) + '\n');
        stringBuffer.append("View右下角距父View左侧的距离:" + String.valueOf(view.getRight()) + '\n');
        textView.setText(stringBuffer.toString().trim());

    }

    @Override
    public void alertAngle() {
        mOtherView.alertShow(mAppActivity.getString(R.string.button_view_angle), mAppActivity.getString(R.string.angle_message));
    }

    @Override
    public void alertRadian() {
        mOtherView.alertShow(mAppActivity.getString(R.string.button_view_radian), mAppActivity.getString(R.string.radian_message));
    }

    @Override
    public void actionAccessibility() {
        Intent actionAccessibilityIntent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
        mAppActivity.startActivity(actionAccessibilityIntent);
    }

    @Override
    public void accessibilityInstall() {
        MyAccessibilityService.INVOKE_TYPE = MyAccessibilityService.TYPE_INSTALL_APP;
        String fileName = Environment.getExternalStorageDirectory() + "/test.apk";
        File installFile = new File(fileName);
        if (installFile.exists()) {
            installFile.delete();
        }
        try {
            installFile.createNewFile();
            FileOutputStream out = new FileOutputStream(installFile);
            byte[] buffer = new byte[512];
            InputStream in = mAppActivity.getAssets().open("test.apk");
            int count;
            while ((count = in.read(buffer)) != -1) {
                out.write(buffer, 0, count);
            }
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
        mAppActivity.startActivity(intent);
    }

    @Override
    public void accessibilityUnInstall() {
        MyAccessibilityService.INVOKE_TYPE = MyAccessibilityService.TYPE_UNINSTALL_APP;
        //包名
        Uri packageURI = Uri.parse("package:com.example.test");
        Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
        mAppActivity.startActivity(uninstallIntent);
    }

    @Override
    public void accessibilityKillApp() {
        MyAccessibilityService.INVOKE_TYPE = MyAccessibilityService.TYPE_KILL_APP;
        Intent killIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        //包名
        Uri packageURI = Uri.parse("package:com.example.test");
        killIntent.setData(packageURI);
        mAppActivity.startActivity(killIntent);
    }

    @Override
    public List<ItemBean> listGetItemData(boolean isRefresh) {
        int page = isRefresh ? 1 : mOtherModel.getPage();
        List<ItemBean> itemBeanList = new ArrayList<>();
        itemBeanList.addAll(mOtherModel.getItemBeanList(page));
        mOtherModel.setPage(page + 1);
        return itemBeanList;
    }
}
